package elementos;

import mapControl.GraphicsMapControl;
import modelLoader.ModelLoader;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;
import com.jmex.effects.particles.ParticleMesh;

import explosionFactory.ExplosionFactory;
/**
 * Clase creadora de objectos "bomba"
 * @author Sebastian Giulietti 69032 
 * @author Adrian Sanchez 64251
 */
public class Bomba extends Elemento implements Runnable {
	
	//Contador de segundos que demora la bomba en explotar
	private float countdown = 3;
	//Booleano que determina si la bomba esta activa 
	private boolean live;
	//control del mapa
	private GraphicsMapControl mapControl;
	//Potencia (distancia) en que explotara
	private int potencia;
	AudioTrack targetSound; //sonido de la explosion
	
	/**
	 * Contructor principal
	 * @param player player
	 * @param mapControl mapcontrol
	 */
	public Bomba(Player player, GraphicsMapControl mapControl) {
		super();		
		potencia = mapControl.getBombPower();
		this.mapControl = mapControl;
		setCol(player.getCol());
		setRow(player.getRow());

		AudioSystem audio = AudioSystem.getSystem();
		targetSound = audio.createAudioTrack(getClass().getResource(
				"/music/explosion.wav"), false);
		targetSound.setVolume(1.0f);
		targetSound.setType(TrackType.POSITIONAL);
		targetSound.setRelative(true);
		targetSound.setWorldPosition(node.getWorldTranslation());

	}
	/**
	 * Metodo que efectua la accion de detonacion de la bomba
	 */
	public void detonar() {
		mapControl.removeBomb(this);
		mapControl.getContadorBombas().detonarBomba();
		live = false;
		targetSound.play();

		explosionCentral(mapControl.getTraslationCentrada(row, col));
		mapControl.detonate(row,col);
		boolean ph = true;
		boolean pv = true;
		
		//Le avisa al mapa que casilleros deben "detonar"
		for (int dist = 1; dist <= potencia; dist++) {

			if (ph) {
				ph = mapControl.detonate((row - dist), col);
				ph = mapControl.detonate((row + dist), col);
			}
			if (pv) {
				pv = mapControl.detonate(row, (col + dist));
				pv = mapControl.detonate(row, (col - dist));
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Animacion de la explosion
	 */
	public void explosionCentral(Vector3f pos) {
		ParticleMesh explosion = ExplosionFactory.getExplosion();
		explosion.setOriginOffset(pos.clone());
		explosion.forceRespawn();
		mapControl.getRootNode().attachChild(explosion);
	}
	/**
	 * Inicio de la vida de la bomba
	 */
	public void run() {

		ExplosionFactory.warmup();

		Node model = ModelLoader.getBombaModel();
		model.setLocalScale(.03f);
		model.setLocalTranslation(0, 1f, 0);
		node.attachChild(model);
		node.setLocalTranslation(mapControl.getTraslationCentrada(row, col));

		mapControl.getRootNode().updateGeometricState(0.0f, true);
	
		mapControl.getContadorBombas().instalarBomba();
		mapControl.addBomb(this);

		mapControl.getRootNode().attachChild(node);
		mapControl.getRootNode().updateRenderState();

		live = true;
		try {
			while (countdown > 0 && live) {
				Thread.sleep(1000);
				countdown--;
			}
			detonar();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}

